package com.company.loganalyzer.controller;

import com.company.loganalyzer.service.LogAnalyzerAgentService;
import com.company.loganalyzer.service.LogAnalyzerAgentService.AgentQueryResponse;
import com.company.loganalyzer.service.LogAnalyzerAgentService.AgentStatus;
import com.company.loganalyzer.service.LogAnalyzerAgentService.ConversationSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * 智能体控制器
 * 
 * 提供智能日志分析的REST API接口：
 * 1. 智能查询处理
 * 2. 会话管理
 * 3. 智能体状态查询
 */
@RestController
@RequestMapping("/api/v1/agent")
public class LogAnalyzerAgentController {
    
    @Autowired
    private LogAnalyzerAgentService agentService;
    
    /**
     * 智能查询接口
     * 
     * 用户使用自然语言提问，智能体自动选择工具并返回分析结果
     * 
     * @param query 自然语言查询
     * @param sessionId 会话ID（可选）
     * @return 智能分析结果
     */
    @PostMapping("/query")
    public AgentQueryResponse query(@RequestParam String query,
                                  @RequestParam(required = false) String sessionId) {
        return agentService.processQuery(query, sessionId);
    }
    
    /**
     * 简化的GET查询接口
     */
    @GetMapping("/query")
    public AgentQueryResponse queryGet(@RequestParam String q,
                                     @RequestParam(required = false) String sessionId) {
        return agentService.processQuery(q, sessionId);
    }
    
    /**
     * 获取智能体状态
     */
    @GetMapping("/status")
    public AgentStatus getStatus() {
        return agentService.getAgentStatus();
    }
    
    /**
     * 获取会话信息
     */
    @GetMapping("/session/{sessionId}")
    public Map<String, Object> getSession(@PathVariable String sessionId) {
        ConversationSession session = agentService.getSession(sessionId);
        
        Map<String, Object> response = new HashMap<>();
        if (session != null) {
            response.put("sessionId", session.getSessionId());
            response.put("createdAt", session.getCreatedAt());
            response.put("turns", session.getTurns().size());
            response.put("status", "active");
        } else {
            response.put("sessionId", sessionId);
            response.put("status", "not_found");
        }
        response.put("timestamp", LocalDateTime.now());
        
        return response;
    }
    
    /**
     * 清除会话
     */
    @DeleteMapping("/session/{sessionId}")
    public Map<String, Object> clearSession(@PathVariable String sessionId) {
        agentService.clearSession(sessionId);
        
        Map<String, Object> response = new HashMap<>();
        response.put("sessionId", sessionId);
        response.put("status", "cleared");
        response.put("timestamp", LocalDateTime.now());
        
        return response;
    }
    
    /**
     * 健康检查
     */
    @GetMapping("/health")
    public Map<String, Object> health() {
        Map<String, Object> response = new HashMap<>();
        AgentStatus status = agentService.getAgentStatus();
        
        response.put("status", "UP");
        response.put("agent", status.getName());
        response.put("version", status.getVersion());
        response.put("llmAvailable", status.isLlmAvailable());
        response.put("availableTools", status.getAvailableTools());
        response.put("activeSessions", status.getActiveSessions());
        response.put("timestamp", LocalDateTime.now());
        
        return response;
    }
}